package com.migration.config;

import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.sql.DataSource;

/**
 * 数据源配置类
 * 配置Druid数据源及JDBC模板
 */
@Configuration
public class DataSourceConfig {

    /**
     * 新数据库数据源
     */
    @Primary
    @Bean(name = "newDataSource")
    @ConfigurationProperties("spring.datasource.druid.new")
    public DataSource newDataSource() {
        return DruidDataSourceBuilder.create().build();
    }

    /**
     * 旧数据库数据源
     */
    @Bean(name = "oldDataSource")
    @ConfigurationProperties("spring.datasource.druid.old")
    public DataSource oldDataSource() {
        return DruidDataSourceBuilder.create().build();
    }

    /**
     * 新数据库JdbcTemplate
     */
    @Primary
    @Bean(name = "newJdbcTemplate")
    public JdbcTemplate newJdbcTemplate(@Qualifier("newDataSource") DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }

    /**
     * 旧数据库JdbcTemplate
     */
    @Bean(name = "oldJdbcTemplate")
    public JdbcTemplate oldJdbcTemplate(@Qualifier("oldDataSource") DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }
} 